package com.mp.ipm2020.mbg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.mp.ipm2020.mbg.model.DeviceInfo;
import com.mp.ipm2020.mbg.mapper.DeviceInfoMapper;
import com.mp.ipm2020.mbg.model.User;
import com.mp.ipm2020.mbg.service.DeviceInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author adidasshe
 * @since 2020-05-18
 */
@Service
public class DeviceInfoServiceImpl extends ServiceImpl<DeviceInfoMapper, DeviceInfo> implements DeviceInfoService {
    @Autowired
    private DeviceInfoMapper deviceInfoMapper;

    @Override
    public List<DeviceInfo> list(String keyword, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<DeviceInfo> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(keyword)) {
            wrapper.like("device_id", keyword)
                    .or()
                    .like("device_name", keyword)
                    .or()
                    .like("device_desc", keyword)
                    .or()
                    .like("address", keyword);
        }
        return deviceInfoMapper.selectList(wrapper);
    }

    @Override
    public int create(DeviceInfo deviceInfo) {
        QueryWrapper<DeviceInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("device_id", deviceInfo.getDeviceId());
        List<DeviceInfo> list = deviceInfoMapper.selectList(wrapper);
        //如果存在该设备则不创建
        if (list.size() > 0) {
            return 2;
        }
        return deviceInfoMapper.insert(deviceInfo);
    }
}
